import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

class WorkbenchPage extends StatefulWidget {
  @override
  _WorkbenchPageState createState() => _WorkbenchPageState();
}

class _WorkbenchPageState extends State<WorkbenchPage> {
  final List<WorkbenchApp> _quickAccessApps = [
    WorkbenchApp(
      title: '智能报表',
      icon: Icons.analytics,
      badge: 2,
      color: Colors.purple,
    ),
    WorkbenchApp(
      title: '项目协同',
      icon: Icons.group_work,
      badge: 0,
      color: Colors.green,
    ),
    // 更多应用...
  ];

  final List<Notice> _notices = [
    Notice(
      title: '季度战略调整通知',
      time: '09:30',
      type: NoticeType.important,
    ),
    Notice(
      title: '会议室使用规范更新',
      time: '昨天',
      type: NoticeType.normal,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 0, // 高度设置为0，就不会显示了呢，2025-03-15
        // title: Text('工作台'),
        // actions: [
        //   IconButton(icon: Icon(Icons.add), onPressed: () {}),
        // ],
      ),
      body: CustomScrollView(
        slivers: [
          _buildAppBar(),
          _buildQuickAccess(),
          _buildNoticeSection(),
          _buildApprovalSection(),
          _buildFunctionGrid(),
        ],
      ),
    );
  }

  SliverAppBar _buildAppBar() {
    return SliverAppBar(
      expandedHeight: 181, // 2025-3-15 这个布局指定高度，导致错误了
      flexibleSpace: FlexibleSpaceBar(
        background: Column(
          children: [
            _buildCompanyHeader(),
            _buildWeatherCard(),
          ],
        ),
      ),
    );
  }

  Widget _buildCompanyHeader() {
    return Container(
      padding: EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(bottom: Radius.circular(16)),
      ),
      child: Row(
        children: [
          CachedNetworkImage(
            imageUrl: 'https://example.com/company_logo.png',
            width: 48,
            height: 48,
          ),
          SizedBox(width: 12),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('尹雷集团', style: TextStyle(fontSize: 18)),
              Text('员工编号：A123456', style: TextStyle(color: Colors.grey)),
            ],
          ),
          Spacer(),
          IconButton(
            icon: Icon(Icons.qr_code_scanner),
            onPressed: () {},
          ),
        ],
      ),
    );
  }

  Widget _buildWeatherCard() {
    return Container(
      margin: EdgeInsets.all(12),
      padding: EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 6,
            offset: Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        children: [
          Icon(Icons.wb_sunny, color: Colors.orange, size: 36),
          SizedBox(width: 12),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('杭州市 晴', style: TextStyle(fontSize: 16)),
              Text('28℃  空气质量：优', style: TextStyle(color: Colors.grey)),
            ],
          ),
          Spacer(),
          Text('09月15日 周三', style: TextStyle(color: Colors.grey)),
        ]
      ),
    );
  }

  Widget _buildQuickAccess() {
    return SliverPadding(
      padding: EdgeInsets.all(12),
      sliver: SliverGrid(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          mainAxisSpacing: 12,
          crossAxisSpacing: 12,
          childAspectRatio: 0.9,
        ),
        delegate: SliverChildBuilderDelegate(
              (context, index) => _buildQuickAccessItem(_quickAccessApps[index]),
          childCount: _quickAccessApps.length,
        ),
      ),
    );
  }

  Widget _buildQuickAccessItem(WorkbenchApp app) {
    return Card(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Badge(
            isLabelVisible: app.badge > 0,
            label: Text(app.badge.toString()),
            child: Icon(app.icon, size: 32, color: app.color),
          ),
          SizedBox(height: 8),
          Text(app.title, style: TextStyle(fontSize: 12)),
        ],
      ),
    );
  }

  Widget _buildNoticeSection() {
    return SliverPadding(
      padding: EdgeInsets.symmetric(horizontal: 12),
      sliver: SliverList(
        delegate: SliverChildListDelegate([
          SectionHeader(title: '企业公告', action: '更多'),
          ..._notices.map((notice) => _buildNoticeItem(notice)),
        ]),
      ),
    );
  }

  Widget _buildNoticeItem(Notice notice) {
    return Card(
      margin: EdgeInsets.only(bottom: 8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
      ),
      child: ListTile(
        leading: Icon(
          Icons.campaign,
          color: notice.type == NoticeType.important
              ? Colors.red
              : Colors.blue,
        ),
        title: Text(notice.title),
        subtitle: Text(notice.time),
        trailing: Icon(Icons.chevron_right),
      ),
    );
  }

  Widget _buildApprovalSection() {
    return SliverPadding(
      padding: EdgeInsets.symmetric(horizontal: 12),
      sliver: SliverToBoxAdapter(
        child: Column(
          children: [
            SectionHeader(title: '审批进度', action: '查看全部'),
            _buildApprovalProgress(),
          ],
        ),
      ),
    );
  }

  Widget _buildApprovalProgress() {
    return Container(
      padding: EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildProgressItem('待审批', 2, Colors.red),
          _buildProgressItem('进行中', 5, Colors.blue),
          _buildProgressItem('已完成', 8, Colors.green),
        ],
      ),
    );
  }

  Widget _buildProgressItem(String title, int count, Color color) {
    return Column(
      children: [
        Text(
          count.toString(),
          style: TextStyle(fontSize: 24, color: color),
        ),
        Text(title, style: TextStyle(color: Colors.grey)),
      ],
    );
  }

  Widget _buildFunctionGrid() {
    return SliverPadding(
      padding: EdgeInsets.all(12),
      sliver: SliverToBoxAdapter(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SectionHeader(title: '常用功能', action: '管理'),
            SizedBox(height: 8),
            Container(
              height: 100,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  _buildFunctionItem('智能考勤', Icons.fingerprint),
                  _buildFunctionItem('文档中心', Icons.description),
                  // 更多功能...
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFunctionItem(String title, IconData icon) {
    return Container(
      width: 100,
      margin: EdgeInsets.only(right: 12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, size: 32, color: Theme.of(context).primaryColor),
          SizedBox(height: 8),
          Text(title),
        ],
      ),
    );
  }
}

// 数据模型和辅助组件
class WorkbenchApp {
  final String title;
  final IconData icon;
  final int badge;
  final Color color;

  WorkbenchApp({
    required this.title,
    required this.icon,
    required this.badge,
    required this.color,
  });
}

class Notice {
  final String title;
  final String time;
  final NoticeType type;

  Notice({
    required this.title,
    required this.time,
    required this.type,
  });
}

enum NoticeType { normal, important }

class SectionHeader extends StatelessWidget {
  final String title;
  final String action;

  const SectionHeader({required this.title, required this.action});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(title, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
          TextButton(
            onPressed: () {},
            child: Text(action),
          ),
        ],
      ),
    );
  }
}
